package com.example.exam18;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    ListView list;
    ArrayList<HashMap<Object,Object>> l=new ArrayList<>();
@SuppressLint("HandlerLeak")
Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
            switch (msg.what){
                case 100:
                    String str= msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(str);
                        JSONObject data = jsonObject.getJSONObject("data");
                        JSONArray datas = data.getJSONArray("datas");
                        for(int i=0;i<datas.length();i++){
                            HashMap<Object, Object> map = new HashMap<>();
                            JSONObject jsonObject1 = datas.getJSONObject(i);
                            String author = jsonObject1.getString("author");
                            String link = jsonObject1.getString("link");
                            String superChapterName = jsonObject1.getString("superChapterName");
                            map.put("author",author);
                            map.put("link",link);
                            map.put("superChapterName",superChapterName);
                            l.add(map);
                        }


                    } catch (JSONException e) {


                    }
                    MyAdapter myAdapter = new MyAdapter(l, MainActivity.this);
                    list.setAdapter(myAdapter);
                    break;

            }
    }
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        list=findViewById(R.id.list);
        new MyThread2("https://www.wanandroid.com/article/list/4/json",MainActivity.this).start();
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"我是第n个item",Toast.LENGTH_SHORT).show();
            }
        });
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                list.removeViewAt(position);
                return false;
            }
        });
    }
}
class MyThread2 extends  Thread{

    String str;
    MainActivity activity;
    public MyThread2(String str,MainActivity activity){
        this.str=str;
        this.activity=activity;
    }
    {
        try {
            URL url = new URL(str);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String string=null;
            while((string=br.readLine())!=null){
                stringBuffer.append(string);
            }
            String s=stringBuffer.toString();
            Message obtain = Message.obtain();
            obtain.obj=s;
            obtain.what=100;
            activity.handler.sendMessage(obtain);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

